﻿using Petaframe.DataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Petaframe.DataAccess.UnitTest.helpers;
using SharpTestsEx;
using System.Data.SqlClient;

namespace Petaframe.DataAccess.UnitTest
{


    /// <summary>
    ///This is a test class for ClassDecomposerTest and is intended
    ///to contain all ClassDecomposerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ClassDecomposerTestSqlServer : ClassPropertyAndInfoTestBase
    {
        /// <summary>
        ///A test for ClassDecomposer Constructor
        ///</summary>
        [TestMethod()]
        public void ClassDecomposerConstructorTest()
        {
            ClassDecomposer target = new ClassDecomposer();
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for ForEntity
        ///</summary>
        public void ForEntityTestHelper<T>()
        {
            ClassDecomposer target = new ClassDecomposer();
            T obj = Activator.CreateInstance<T>();

            TableInfo actual;
            actual = target.ForEntity<T>(obj, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            actual.Should().Be.EqualTo(this.expectedTableInfo);
        }

        [TestMethod()]
        public void ForEntityTest()
        {
            ForEntityTestHelper<Category>();
        }

        /// <summary>
        ///A test for ForObject
        ///</summary>
        [TestMethod()]
        public void ForObjectTest()
        {
            ClassDecomposer target = new ClassDecomposer();
            object obj = new Category();

            TableInfo actual;
            actual = target.ForObject(obj, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            Assert.AreEqual(expectedTableInfo, actual);

        }

        /// <summary>
        ///A test for ForType
        ///</summary>
        [TestMethod()]
        public void ForTypeTest()
        {
            ClassDecomposer target = new ClassDecomposer();
            Type tip = new Category().GetType();

            TableInfo actual;
            actual = target.ForType(tip, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            Assert.AreEqual(expectedTableInfo, actual);

        }

        [TestMethod]
        public void HasPrimaryKeyShouldBeTrue()
        {
            ClassDecomposer target = new ClassDecomposer();
            TableInfo actual = target.ForEntity(testArgument, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            actual.HasPrimaryKey.Should().Be.True();
        }

        [TestMethod]
        public void TableNameShouldBeInBrackets()
        {
            ClassDecomposer target = new ClassDecomposer();
            TableInfo actual = target.ForEntity(testArgument, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            actual.TableName.Should().Be.EqualTo("[Categories]");
        }
      

        [TestMethod]
        public void PropertiesCountShouldBeThree()
        {
            ClassDecomposer target = new ClassDecomposer();
            TableInfo actual = target.ForEntity(testArgument, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            actual.Properties.Count.Should().Be.EqualTo(3);
        }

        [TestMethod]
        public void FirstPropertyShouldBeID()
        {
            ClassDecomposer target = new ClassDecomposer();
            TableInfo actual = target.ForEntity(testArgument, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            actual.Properties[0].PropertyName.Should().Be.EqualTo("CategoryID");
        }

        [TestMethod]
        public void FirstPropertyShouldBePrimaryKey()
        {
            ClassDecomposer target = new ClassDecomposer();
            TableInfo actual = target.ForEntity(testArgument, new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider);
            actual.Properties[0].PrimaryKey.Should().Be.True();
        }

        [TestMethod]
        public void NullTableNameAttributeThrows()
        {
            ClassDecomposer target = new ClassDecomposer();
            target.Executing(z => z.ForEntity(new CategoryWithoutTableNameAttribute(), new SqlServerParameterBuilder(), new SqlServerEscapeSqlIdentifier(), _sqlProvider)).Throws();          
           
        }
    }
}
